package com.lanou.musicbox.adapter;

import android.content.Context;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.lanou.musicbox.R;

import com.lanou.musicbox.bean.Songs;
import com.lanou.musicbox.utils.VolleySingleton;

import java.util.List;

/**
 * Created by 赵宝龙 on 15/10/21.
 * 歌单页面adapter
 */
public class SongsAdapter extends BaseAdapter {
    private List<Songs.ContentEntity> datas;
    private Context mContext;
    private VolleySingleton volleySingleton;
    private ImageLoader imageLoader;



    public SongsAdapter(Context c, List<Songs.ContentEntity> datas) {
        mContext = c;

        this.datas = datas;
        Log.e("ZBL", "*****************" + datas.get(0).getCollectnum());
        volleySingleton = VolleySingleton.getVolleySingleton(c);
        imageLoader = volleySingleton.getImageLoader();
    }

    @Override
    public int getCount() {
        return datas != null && datas.size() > 0 ? datas.size() : 0;
    }

    @Override
    public Object getItem(int i) {
        return datas != null && datas.size() > 0 ? datas.get(i) : null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view == null) {
            view = LayoutInflater.from(mContext).inflate(R.layout.songs_item, null);
            holder = new ViewHolder();
            holder.pic = (ImageView) view.findViewById(R.id.pic);
            holder.listenum = (TextView) view.findViewById(R.id.listenum);
            holder.title = (TextView) view.findViewById(R.id.title);
            holder.tag = (TextView) view.findViewById(R.id.tag);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        Songs.ContentEntity s = datas.get(i);
        ImageLoader.ImageListener listener = ImageLoader.getImageListener(holder.pic,R.mipmap.lmbsn,R.mipmap.lmbsn);

        imageLoader.get(s.getPic_300(),listener);
        if (s != null) {
            holder.listenum.setText(s.getListenum());
            holder.title.setText(s.getTitle());
            holder.tag.setText(s.getTag());
        }
        return view;
    }

    private class ViewHolder {
        private ImageView pic;
        private TextView listenum;
        private TextView title;
        private TextView tag;
    }

    public String getUrl(int position){
        return datas.get(position).getListid();
    }
}
